﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;
using System.Globalization;
using System.IO;
using System.Web;

namespace QLKTX1._0.GUI.QLSV
{
    public partial class FrmNhanVien : Form

    {
        private TreeNode root;


        public FrmNhanVien()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void FrmNhanVien_Load(object sender, EventArgs e)
        {
            //load tree view  
           
            //grNhanVien.Enabled = false;//

            //txtTinhTrang.Text = "Đương chức";
            //Dat chuan cho do cao va do rong cua anh
            anhNV.Height = 245;
            anhNV.Width = 175;

            btnLuu.Enabled = false;

            this.showTree();
            //----------Hien thi ten HocVan, Ton Giao, Dan Toc len Combobox--------------- 
            
            cmbHocVan.DataSource = HocVanDAO.dsHocVan();
            cmbHocVan.DisplayMember = "tenHocVan";
            cmbHocVan.ValueMember = "maHocVan";

            cmbDanToc.DataSource = DanTocDAO.dsDanToc();
            cmbDanToc.DisplayMember = "tenDanToc";
            cmbDanToc.ValueMember = "maDanToc";

            cmbTonGiao.DataSource = TonGiaoDAO.dsTonGiao();
            cmbTonGiao.DisplayMember = "tenTonGiao";
            cmbTonGiao.ValueMember = "maTonGiao";

            cmbXemTheoNH.DataSource = NamHocDAO.dsNamHoc();
            cmbXemTheoNH.DisplayMember = "tenNamHoc";
            cmbXemTheoNH.ValueMember = "maNamHoc";
            cmbXemTheoNH.Text = "Tất cả";
         
        }

        private void showTree()
        {
            tvdsnv.Nodes.Clear();

            root = tvdsnv.Nodes.Add("Danh Sách Nhân Viên"); // Not goc

            root.Tag = 0;

            int f = 0;

            foreach (NhanVienDTO value in NhanVienDAO.dsNhanVien())
            {
                TreeNode c = new TreeNode();
                c.Text = value.TenNhanVien; // hien thi ten 
                c.Tag = value.MaNhanVien; // lay ma
              
                tvdsnv.Nodes[0].Nodes.Add(c);//hoi
                // -- xu ly chon node dau tieu
                if (f == 0)
                {
                    tvdsnv.SelectedNode = c;
                }
                tvdsnv.ExpandAll();//hoi
                f++;

            }
        }

        private void tvdsnv_AfterSelect(object sender, TreeViewEventArgs e)
        {

            String id = e.Node.Tag.ToString();

            if (!id.Equals("0"))
            {

                NhanVienDTO nv = NhanVienDAO.getInfoNhanVien(id);

                txtMaNhanVien.Text = nv.MaNhanVien;
                txtTenNhanVien.Text = nv.TenNhanVien;
                txtDiaChi.Text = nv.DiaChi;
                txtDienThoai.Text = nv.DienThoai;
                                
                if (nv.GioiTinh == 1)
                {
                    optNam.Checked = true;
                }
                else optNu.Checked = true;

                txtNgaySinh.Text = nv.NgaySinh.ToString("dd/MM/yyyy");
                //----------Tinh trang chua xu ly
                if (nv.TinhTrang == 1)
                {
                    checkBox1.Checked = true;
                }
                else checkBox1.Checked = false;

                txtNgayBatDau.Text = nv.NgayBatDau.ToString("dd/MM/yyyy");
                cmbDanToc.Text = nv.MaDanToc.TenDanToc;
                cmbTonGiao.Text = nv.MaTonGiao.TenTonGiao;
                cmbHocVan.Text = nv.MaHocVan.TenHocVan;
//
              //  txtMatKhau.Text = nv.MatKhau;


                string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));

                if (!nv.HinhAnh.Equals(""))
                {
                    string hinhAnh = path + "FILES\\" + nv.HinhAnh; // duong dan den hinh anh
                    anhNV.Image = Image.FromFile(hinhAnh); // gan vao box
                    anhNV.Height = 275;
                    anhNV.Width = 183;

                }
                else anhNV.Image = Image.FromFile(path + "FILES\\noimage.jpg");

                txtIndex.Text = e.Node.Index.ToString();
                
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                
                txtTinhTrang.Text = "Nghỉ việc";
            }
            else
            {
                txtTinhTrang.Text = "Đương chức";
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Thêm"))
            {

                btnThem.Enabled = true;
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = false;
                btnLuu.Enabled = true;

                // reset cac thuoc tinh
                anhNV.Image = null;
                grNhanVien.Enabled = true;
                grNhanVien.Text  = "";
                //kich hoat text
                foreach (Control gb in this.Controls)
                {
                    if (gb is GroupBox)
                    {

                        foreach (Control tb in gb.Controls)
                        {
                            if (tb is TextBox)
                            {
                                //here is where you access all the textboxs.
                                tb.Text = "";
                            }
                        }

                    }
                }
                
                txtMaNhanVien.Text = Service.generateNoAuto("nhanVien", "maNhanVien", "NV",100);
                btnThem.Text = "Hủy bỏ";
            }
            else
            {
                btnThem.Text = "Thêm";
                btnThem.Enabled = true;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
                grNhanVien.Enabled = false;
                            
            } 
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtTenNhanVien.Text.Equals("") || txtDienThoai.Text.Equals("") || txtDiaChi.Text.Equals("") || txtNgaySinh.Text.Equals("") || txtDienThoai.Text.Equals("") || txtNgayBatDau.Text .Equals("") )
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    String Id = txtMaNhanVien .Text ;
                    String hoten = txtTenNhanVien.Text;
                    string dt = txtDienThoai.Text;
                    string dc = txtDiaChi.Text;
                   // string mk = txtMatKhau.Text;
                    int gt = 1;

                    if (optNu.Checked)
                    {
                        gt = 0;
                    }

                    //int tt = 1;
                   // if (checkBox1.Checked)
                   // {
                   //     tt = 0;
                   // }

                    int tt ;
                    if (checkBox1.Checked)
                    {
                        tt = 1;
                        txtTinhTrang.Text = "Nghỉ việc";
                    }
                    else
                    {
                        tt = 0;
                        txtTinhTrang.Text = "Đương chức";
                    }

                    NhanVienDTO nv = new NhanVienDTO();

                    nv.MaNhanVien = Id;
                    nv.TenNhanVien = hoten;
                    nv.DiaChi = dc;
                    nv.DienThoai = dt;
                    nv.GioiTinh = gt;
                    nv.TinhTrang = tt;
                    //nv.MatKhau = mk;
                    nv.NgayBatDau = DateTime.ParseExact(txtNgayBatDau .Text , "dd/MM/yyyy", null);
                    nv.NgaySinh = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", null);

                    string  hocvan = cmbHocVan.SelectedValue.ToString();
                    int tongiao = Int32.Parse(cmbTonGiao.SelectedValue.ToString());
                    int dantoc = Int32.Parse(cmbDanToc.SelectedValue.ToString());

                    HocVanDTO hv = new HocVanDTO();
                    hv.MaHocVan = hocvan;
                    nv.MaHocVan = hv;
                   
                    DanTocDTO odt = new DanTocDTO();
                    odt.MaDanToc = dantoc;
                    nv.MaDanToc = odt;

                    TonGiaoDTO otg = new TonGiaoDTO();
                    otg.MaTonGiao = tongiao;
                    nv.MaTonGiao = otg;

                   
                    // lay hinh anh tu box

                    if (anhNV.Image != null)
                    {

                        Image image = Image.FromFile(ofdAnhNV.FileName); // Lay hinh anh tu dialog

                        string filename = Path.GetFileName(ofdAnhNV.FileName); // lay duong dan
                                               

                        string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename; // tao ten file moi
                                             

                        string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin")); // noi luu

                        image.Save(path + "\\FILES\\" + strtemp); // thuc hien luu xuong thu muc

                        nv.HinhAnh = strtemp; //lay ten file hinh anh sv
                    }
                    else nv.HinhAnh = "";


                    NhanVienDAO.themNhanVien(nv);
                    
                    TreeNode c = new TreeNode();
                    c.Text = txtTenNhanVien.Text;
                    c.Tag = txtMaNhanVien.Text;
                    tvdsnv.Nodes[0].Nodes.Add(c);

                    btnThem.Text = "Thêm";
                    btnThem.Enabled = true;
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnLuu.Enabled = false;
                    //this.FrmQLHSSV_Load(sender, e);

                }
            }
            else if (btnCapNhat.Text.Equals("Hủy bỏ"))
            {
                if (txtTenNhanVien.Text.Equals("") || txtDienThoai.Text.Equals("") || txtDiaChi.Text.Equals("") || txtNgaySinh.Text.Equals("") || txtDienThoai.Text.Equals("") || txtNgayBatDau.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    String Id = txtMaNhanVien.Text;
                    String hoten = txtTenNhanVien.Text;
                    string dt = txtDienThoai.Text;
                    string dc = txtDiaChi.Text;
                   // string mk = txtMatKhau.Text;
                    int gt = 1;

                    if (optNu.Checked)
                    {
                        gt = 0;
                    }

                    int tt;
                    if (checkBox1.Checked)
                    {
                        tt = 1;
                        txtTinhTrang.Text = "Nghỉ việc";
                    }
                    else
                    {
                        tt = 0;
                        txtTinhTrang.Text = "Đương chức";
                    }

                    NhanVienDTO nv = new NhanVienDTO();

                    nv.MaNhanVien = Id;
                    nv.TenNhanVien = hoten;
                    nv.DiaChi = dc;
                    nv.DienThoai = dt;
                    nv.GioiTinh = gt;
                    nv.TinhTrang = tt;
                    nv.NgayBatDau = DateTime.ParseExact(txtNgayBatDau.Text, "dd/MM/yyyy", null);
                    nv.NgaySinh = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", null);
                   // nv.MatKhau = mk;

                    string hocvan = cmbHocVan.SelectedValue.ToString();
                    int tongiao = Int32.Parse(cmbTonGiao.SelectedValue.ToString());
                    int dantoc = Int32.Parse(cmbDanToc.SelectedValue.ToString());

                    HocVanDTO hv = new HocVanDTO();
                    hv.MaHocVan = hocvan;
                    nv.MaHocVan = hv;

                    DanTocDTO odt = new DanTocDTO();
                    odt.MaDanToc = dantoc;
                    nv.MaDanToc = odt;

                    TonGiaoDTO otg = new TonGiaoDTO();
                    otg.MaTonGiao = tongiao;
                    nv.MaTonGiao = otg;


                    // lay hinh anh tu box

                    if (anhNV.Image != null)
                    {

                        if (!ofdAnhNV.FileName.Equals("-1")) // neu chon hinh anh moi
                        {
                            Image image = Image.FromFile(ofdAnhNV.FileName); // Lay hinh anh tu dialog

                            string filename = Path.GetFileName(ofdAnhNV.FileName); // lay duong dan

                            //string ext = Path.GetExtension(filename);

                            string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename; // tao ten file moi

                            //lbAnhSV.Text = strtemp;

                            string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin")); // noi luu

                            image.Save(path + "\\FILES\\" + strtemp); // thuc hien luu xuong thu muc

                            nv.HinhAnh = strtemp; //lay ten file hinh anh sv
                        }
                    }

                    NhanVienDAO.capNhatNhanVien(nv);
                    

                    // huy bo
                    btnCapNhat.Text = "Cập Nhật";
                    btnThem.Enabled = true;
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnLuu.Enabled = false;
                   
                    this.showTree();
                   
                }
            }
        }

        private void anhNV_Click(object sender, EventArgs e)
        {
            ofdAnhNV.FileName = "Chọn ảnh (3x4)";
            ofdAnhNV.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";

            if (ofdAnhNV.ShowDialog() == DialogResult.OK)
            {

                anhNV.Image = Image.FromFile(ofdAnhNV.FileName);

                anhNV.Height = 244;

                anhNV.Width = 175;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnCapNhat.Text.Equals("Cập Nhật"))
            {
                tvdsnv.Enabled = true;
                btnThem.Enabled = false;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = false;
                btnLuu.Enabled = true;
               
                grNhanVien.Enabled = true;
               
                btnCapNhat.Text = "Hủy bỏ";
            }
            else
            {
                tvdsnv.Enabled = true;
                btnCapNhat.Text = "Cập Nhật";
                btnThem.Enabled = true;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
                this.showTree();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                String id = txtMaNhanVien.Text;
                NhanVienDTO nv = new NhanVienDTO();
                nv.MaNhanVien = id;

                NhanVienDAO.xoaNhanVien(nv);
                
                tvdsnv.Nodes[0].Nodes.RemoveAt(Int32.Parse(txtIndex.Text));
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
           //FrmTimKiemNV f = new FrmTimKiemNV();
           // f.ShowDialog(this) == DialogResult.OK

         //    if (f.ShowDialog(this) == DialogResult.OK)
        //    {


               // if (!f.MaSV.Equals(""))
              //  {
                //    this.seachByOption(f.MaSV, 1);
              //  }
              //  else
              //  {
                    // tim theo ten                  

             //       this.seachByOption(f.MaSV, 2);
                //}


            //}

          // f.Dispose();
        }

        private void cmbXemTheoNH_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }

        private void tabPage5_Click(object sender, EventArgs e)
        {

        }
    }
}
